﻿using MaterialDesignThemes.Wpf;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 基于WPF的电缆采样数据分析系统.Models.Enums;
using 基于WPF的电缆采样数据分析系统.Models.RefWav;
using 基于WPF的电缆采样数据分析系统.Service;
using 基于WPF的电缆采样数据分析系统.Tools.LangugeTool;

namespace 基于WPF的电缆采样数据分析系统.ViewModels.TDRWork
{
    public class SingleTestUCViewModel : BindableBase, IDialogHostAware
    {
        // 主机名
        private const string DialogHostName = "RootDialog";

        #region 确定
        /// <summary>
        /// 确定命令
        /// </summary>
        public DelegateCommand SaveCommand { get; set; }

        /// <summary>
        /// 确定方法
        /// </summary>
        private void SaveFun()
        {

        }
        #endregion

        #region 取消
        /// <summary>
        /// 取消命令
        /// </summary>
        public DelegateCommand CancelCommand { get; set; }

        /// <summary>
        /// 取消方法
        /// </summary>
        private void CancelFun()
        {
            if (DialogHost.IsDialogOpen(DialogHostName))
            {
                DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.No));
            }
        }
        #endregion

        /// <summary>
        /// 打开过程中
        /// </summary>
        /// <param name="parameters"></param>
        public void OnDialogOpening(IDialogParameters parameters)
        {
            // 初始化通道和参考波形状态
            InitChannelAndReferenceWaveformStatus();
        }

        /// <summary>
        /// 构造器
        /// </summary>
        public SingleTestUCViewModel()
        {
            // 取消命令
            CancelCommand = new DelegateCommand(CancelFun);

            // 绑定初始通道单次测试命令
            TestWavformPCmm = new DelegateCommand<PassageType?>(TestWavformPFun);

            // 绑定参考波形单次测试命令
            TestWavformRCmm = new DelegateCommand<RefWavformType?>(TestWavformRFun);
        }


        #region 容器状态
        /// <summary>
        /// 初始通道一状态
        /// </summary>
        private string _ChannelOneStatus = LangugeConvertor.GetValue("Empty");

        /// <summary>
        /// 初始通道一状态
        /// </summary>
        public string ChannelOneStatus
        {
            get { return _ChannelOneStatus; }
            set
            {
                _ChannelOneStatus = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 初始通道二状态
        /// </summary>
        private string _ChannelTwoStatus = LangugeConvertor.GetValue("Empty");

        /// <summary>
        /// 初始通道二状态
        /// </summary>
        public string ChannelTwoStatus
        {
            get { return _ChannelTwoStatus; }
            set
            {
                _ChannelTwoStatus = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 初始通道三状态
        /// </summary>
        private string _ChannelThreeStatus = LangugeConvertor.GetValue("Empty");

        /// <summary>
        /// 初始通道三状态
        /// </summary>
        public string ChannelThreeStatus
        {
            get { return _ChannelThreeStatus; }
            set
            {
                _ChannelThreeStatus = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 参考波形一状态
        /// </summary>
        private string _ReferenceWaveformOneStatus = LangugeConvertor.GetValue("Empty");

        /// <summary>
        /// 参考波形一状态
        /// </summary>
        public string ReferenceWaveformOneStatus
        {
            get { return _ReferenceWaveformOneStatus; }
            set
            {
                _ReferenceWaveformOneStatus = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 参考波形二状态
        /// </summary>
        private string _ReferenceWaveformTwoStatus = LangugeConvertor.GetValue("Empty");

        /// <summary>
        /// 参考波形二状态
        /// </summary>
        public string ReferenceWaveformTwoStatus
        {
            get { return _ReferenceWaveformTwoStatus; }
            set
            {
                _ReferenceWaveformTwoStatus = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 参考波形三状态
        /// </summary>
        private string _ReferenceWaveformThreeStatus = LangugeConvertor.GetValue("Empty");

        /// <summary>
        /// 参考波形三状态
        /// </summary>
        public string ReferenceWaveformThreeStatus
        {
            get { return _ReferenceWaveformThreeStatus; }
            set
            {
                _ReferenceWaveformThreeStatus = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 参考波形四状态
        /// </summary>
        private string _ReferenceWaveformFourStatus = LangugeConvertor.GetValue("Empty");

        /// <summary>
        /// 参考波形四状态
        /// </summary>
        public string ReferenceWaveformFourStatus
        {
            get { return _ReferenceWaveformFourStatus; }
            set
            {
                _ReferenceWaveformFourStatus = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 参考波形五状态
        /// </summary>
        private string _ReferenceWaveformFiveStatus = LangugeConvertor.GetValue("Empty");

        /// <summary>
        /// 参考波形五状态
        /// </summary>
        public string ReferenceWaveformFiveStatus
        {
            get { return _ReferenceWaveformFiveStatus; }
            set
            {
                _ReferenceWaveformFiveStatus = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 参考波形六状态
        /// </summary>
        private string _ReferenceWaveformSixStatus = LangugeConvertor.GetValue("Empty");

        /// <summary>
        /// 参考波形六状态
        /// </summary>
        public string ReferenceWaveformSixStatus
        {
            get { return _ReferenceWaveformSixStatus; }
            set
            {
                _ReferenceWaveformSixStatus = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 参考波形七状态
        /// </summary>
        private string _ReferenceWaveformSevenStatus = LangugeConvertor.GetValue("Empty");

        /// <summary>
        /// 参考波形七状态
        /// </summary>
        public string ReferenceWaveformSevenStatus
        {
            get { return _ReferenceWaveformSevenStatus; }
            set
            {
                _ReferenceWaveformSevenStatus = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 参考波形八状态
        /// </summary>
        private string _ReferenceWaveformEightStatus = LangugeConvertor.GetValue("Empty");

        /// <summary>
        /// 参考波形八状态
        /// </summary>
        public string ReferenceWaveformEightStatus
        {
            get { return _ReferenceWaveformEightStatus; }
            set
            {
                _ReferenceWaveformEightStatus = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 初始化通道和参考波形状态
        /// </summary>
        private void InitChannelAndReferenceWaveformStatus()
        {
            if (App.ChannelDataList[0].PData != null)
            {
                ChannelOneStatus = LangugeConvertor.GetValue("HaveBeenImported");
                IsChannelOneShow = true;
            }

            if (App.ChannelDataList[1].PData != null)
            {
                ChannelTwoStatus = LangugeConvertor.GetValue("HaveBeenImported");
                IsChannelTwoShow = true;
            }

            if (App.ChannelDataList[2].PData != null)
            {
                ChannelThreeStatus = LangugeConvertor.GetValue("HaveBeenImported");
                IsChannelThreeShow = true;
            }

            if (GlobalVariableEdit.R1WavList != null)
            {
                ReferenceWaveformOneStatus = LangugeConvertor.GetValue("HaveBeenImported");
                IsRefWavformOneShow = true;
            }

            if (GlobalVariableEdit.R2WavList != null)
            {
                ReferenceWaveformTwoStatus = LangugeConvertor.GetValue("HaveBeenImported");
                IsRefWavformTwoShow = true;
            }

            if (GlobalVariableEdit.R3WavList != null)
            {
                ReferenceWaveformThreeStatus = LangugeConvertor.GetValue("HaveBeenImported");
                IsRefWavformThreeShow = true;
            }

            if (GlobalVariableEdit.R4WavList != null)
            {
                ReferenceWaveformFourStatus = LangugeConvertor.GetValue("HaveBeenImported");
                IsRefWavformFourShow = true;
            }

            if (GlobalVariableEdit.R5WavList != null)
            {
                ReferenceWaveformFiveStatus = LangugeConvertor.GetValue("HaveBeenImported");
                IsRefWavformFiveShow = true;
            }

            if (GlobalVariableEdit.R6WavList != null)
            {
                ReferenceWaveformSixStatus = LangugeConvertor.GetValue("HaveBeenImported");
                IsRefWavformSixShow = true;
            }

            if (GlobalVariableEdit.R7WavList != null)
            {
                ReferenceWaveformSevenStatus = LangugeConvertor.GetValue("HaveBeenImported");
                IsRefWavformSevenShow = true;
            }

            if (GlobalVariableEdit.R8WavList != null)
            {
                ReferenceWaveformEightStatus = LangugeConvertor.GetValue("HaveBeenImported");
                IsRefWavformEightShow = true;
            }
        }
        #endregion


        #region 是否显示测试按钮

        /// <summary>
        /// 初始通道一测试按钮
        /// </summary>
        private bool _IsChannelOneShow = false;

        /// <summary>
        /// 初始通道一测试按钮
        /// </summary>
        public bool IsChannelOneShow
        {
            get { return _IsChannelOneShow; }
            set {
                _IsChannelOneShow = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 初始通道二测试按钮
        /// </summary>
        private bool _IsChannelTwoShow = false;

        /// <summary>
        /// 初始通道二测试按钮
        /// </summary>
        public bool IsChannelTwoShow
        {
            get { return _IsChannelTwoShow; }
            set
            {
                _IsChannelTwoShow = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 初始通道三测试按钮
        /// </summary>
        private bool _IsChannelThreeShow = false;

        /// <summary>
        /// 初始通道三测试按钮
        /// </summary>
        public bool IsChannelThreeShow
        {
            get { return _IsChannelThreeShow; }
            set
            {
                _IsChannelThreeShow = value;
                RaisePropertyChanged();
            }
        }


        /// <summary>
        /// 参考波形一测试按钮
        /// </summary>
        private bool _IsRefWavformOneShow = false;

        /// <summary>
        /// 参考波形一测试按钮
        /// </summary>
        public bool IsRefWavformOneShow
        {
            get { return _IsRefWavformOneShow; }
            set { 
                _IsRefWavformOneShow = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 参考波形二测试按钮
        /// </summary>
        private bool _IsRefWavformTwoShow = false;

        /// <summary>
        /// 参考波形二测试按钮
        /// </summary>
        public bool IsRefWavformTwoShow
        {
            get { return _IsRefWavformTwoShow; }
            set
            {
                _IsRefWavformTwoShow = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 参考波形三测试按钮
        /// </summary>
        private bool _IsRefWavformThreeShow = false;

        /// <summary>
        /// 参考波形三测试按钮
        /// </summary>
        public bool IsRefWavformThreeShow
        {
            get { return _IsRefWavformThreeShow; }
            set
            {
                _IsRefWavformThreeShow = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 参考波形四测试按钮
        /// </summary>
        private bool _IsRefWavformFourShow = false;

        /// <summary>
        /// 参考波形四测试按钮
        /// </summary>
        public bool IsRefWavformFourShow
        {
            get { return _IsRefWavformFourShow; }
            set
            {
                _IsRefWavformFourShow = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 参考波形五测试按钮
        /// </summary>
        private bool _IsRefWavformFiveShow = false;

        /// <summary>
        /// 参考波形五测试按钮
        /// </summary>
        public bool IsRefWavformFiveShow
        {
            get { return _IsRefWavformFiveShow; }
            set
            {
                _IsRefWavformFiveShow = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 参考波形六测试按钮
        /// </summary>
        private bool _IsRefWavformSixShow = false;

        /// <summary>
        /// 参考波形六测试按钮
        /// </summary>
        public bool IsRefWavformSixShow
        {
            get { return _IsRefWavformSixShow; }
            set
            {
                _IsRefWavformSixShow = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 参考波形七测试按钮
        /// </summary>
        private bool _IsRefWavformSevenShow = false;

        /// <summary>
        /// 参考波形七测试按钮
        /// </summary>
        public bool IsRefWavformSevenShow
        {
            get { return _IsRefWavformSevenShow; }
            set
            {
                _IsRefWavformSevenShow = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 参考波形八测试按钮
        /// </summary>
        private bool _IsRefWavformEightShow = false;

        /// <summary>
        /// 参考波形八测试按钮
        /// </summary>
        public bool IsRefWavformEightShow
        {
            get { return _IsRefWavformEightShow; }
            set
            {
                _IsRefWavformEightShow = value;
                RaisePropertyChanged();
            }
        }
        #endregion


        #region 初始通道单次测试命令
        public DelegateCommand<PassageType?> TestWavformPCmm { get; set; }

        private void TestWavformPFun(PassageType? P)
        {
            if (DialogHost.IsDialogOpen(DialogHostName))
            {
                UpdateWavList updateWavList = new UpdateWavList();

                switch (P)
                {
                    case PassageType.ChannelOne:
                        updateWavList.WavList = App.ChannelDataList[0].PData;
                        RealTimeOscilloscopeViewModel.Instance.SingleTestWavform(updateWavList);
                        break;
                    case PassageType.ChannelTwo:
                        updateWavList.WavList = App.ChannelDataList[1].PData;
                        RealTimeOscilloscopeViewModel.Instance.SingleTestWavform(updateWavList);
                        break;
                    case PassageType.ChannelThree:
                        updateWavList.WavList = App.ChannelDataList[2].PData;
                        RealTimeOscilloscopeViewModel.Instance.SingleTestWavform(updateWavList);
                        break;
                }

                DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.Yes));
            }

        }
        #endregion


        #region 参考波形单次测试命令
        public DelegateCommand<RefWavformType?> TestWavformRCmm { get; set; }


        private void TestWavformRFun(RefWavformType? R)
        {
            if (DialogHost.IsDialogOpen(DialogHostName))
            {

                switch (R)
                {
                    case RefWavformType.RefWavformOne:
                        RealTimeOscilloscopeViewModel.Instance.SingleTestWavform(RefreshWav(GlobalVariableEdit.R1WavList));
                        break;
                    case RefWavformType.RefWavformTwo:
                        RealTimeOscilloscopeViewModel.Instance.SingleTestWavform(RefreshWav(GlobalVariableEdit.R2WavList));
                        break;
                    case RefWavformType.RefWavformThree:
                        RealTimeOscilloscopeViewModel.Instance.SingleTestWavform(RefreshWav(GlobalVariableEdit.R3WavList));
                        break;
                    case RefWavformType.RefWavformFour:
                        RealTimeOscilloscopeViewModel.Instance.SingleTestWavform(RefreshWav(GlobalVariableEdit.R4WavList));
                        break;
                    case RefWavformType.RefWavformFive:
                        RealTimeOscilloscopeViewModel.Instance.SingleTestWavform(RefreshWav(GlobalVariableEdit.R5WavList));
                        break;
                    case RefWavformType.RefWavformSix:
                        RealTimeOscilloscopeViewModel.Instance.SingleTestWavform(RefreshWav(GlobalVariableEdit.R6WavList));
                        break;
                    case RefWavformType.RefWavformSeven:
                        RealTimeOscilloscopeViewModel.Instance.SingleTestWavform(RefreshWav(GlobalVariableEdit.R7WavList));
                        break;
                    case RefWavformType.RefWavformEight:
                        RealTimeOscilloscopeViewModel.Instance.SingleTestWavform(RefreshWav(GlobalVariableEdit.R8WavList));
                        break;
                }

                DialogHost.Close(DialogHostName, new DialogResult(ButtonResult.Yes));
            }
        }

        private UpdateWavList RefreshWav(UpdateWavList updateWavList)
        {
            UpdateWavList newUpdateWavList = new UpdateWavList();
            List<float> newWavList = new List<float>();
            for (int i = 0; i < updateWavList.WavList.Count; i++)
            {
                newWavList.Add(updateWavList.WavList[i]);
            }

            newUpdateWavList.WavList = newWavList;
            newUpdateWavList.XAxisTranslation = updateWavList.XAxisTranslation;
            newUpdateWavList.YAxisTranslation = updateWavList.YAxisTranslation;

            return newUpdateWavList;
        }

        #endregion
    }
}
